package com.yejing.redisdemo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Demo3 {
    private static volatile JedisPool jedisPool = null;

    private Demo3() {
    }

    public static JedisPool getJedisPoolInstance() {
        // 使用双重检查创建单例
        if (null == jedisPool) {
            synchronized (Demo3.class) {
                if (null == jedisPool) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(10);
                    poolConfig.setMaxIdle(10);
                    poolConfig.setMinIdle(2);
                    poolConfig.setMaxWaitMillis(30 * 1000);
                    poolConfig.setTestOnBorrow(true);
                    poolConfig.setTestOnReturn(true);
                    poolConfig.setTimeBetweenEvictionRunsMillis(10 * 1000);
                    poolConfig.setMinEvictableIdleTimeMillis(30 * 1000);
                    poolConfig.setNumTestsPerEvictionRun(-1);
                    U u = new U("43.136.67.62", 6379, "default", "123456");
                    jedisPool = new JedisPool(poolConfig, u.host,u.port,u.user,u.password);
                }
            }
        }
        return jedisPool;
    }

    private final static class U{
        private String host;
        private Integer port;
        private String user;
        private String password;

        public U(String host, Integer port, String user, String password) {
            this.host = host;
            this.port = port;
            this.user = user;
            this.password = password;
        }
    }

    public static void main(String[] args) {
        JedisPool jedisPoolInstance = getJedisPoolInstance();
        Jedis jedis = jedisPoolInstance.getResource();
        String ping = jedis.ping();
        System.out.println(ping);
    }
}
